package com.ruoyi.production.service.impl;

import java.beans.Transient;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.production.domain.ProductionPlanDetail;
import com.ruoyi.production.domain.vo.ProductionAndSaleVo;
import com.ruoyi.production.domain.vo.SaleOrderDetailVo;
import com.ruoyi.production.mapper.ProductionPlanDetailMapper;
import com.ruoyi.sale.domain.SaleOrder;
import com.ruoyi.sale.mapper.SaleOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.production.mapper.ProductionPlanMapper;
import com.ruoyi.production.domain.ProductionPlan;
import com.ruoyi.production.service.IProductionPlanService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 生产计划Service业务层处理
 *
 * @author jgb
 * @date 2024-10-25
 */
@Service
public class ProductionPlanServiceImpl implements IProductionPlanService
{
    @Autowired
    private ProductionPlanMapper productionPlanMapper;
    @Autowired
    private ProductionPlanDetailMapper productionPlanDetailMapper;
    @Autowired
    private SaleOrderMapper saleOrderMapper;

    @Override
    public List<ProductionPlan> getPlans(ProductionPlan productionPlan) {
        return productionPlanMapper.selectPlansAll(productionPlan);
    }

    @Override
    public List<SaleOrderDetailVo> getSaleOrders() {
        List<SaleOrderDetailVo> saleOrderDetailVos = productionPlanMapper.selectSaleOrders();
        return saleOrderDetailVos;
    }

    @Override
    public List<ProductionAndSaleVo> getProductionPlanAndSaleList(ProductionPlan productionPlan, String saleOrderNo,String customerName) {
        return productionPlanMapper.selectProductionPlanAndSaleList(productionPlan, saleOrderNo, customerName);
    }

    /**
     * 查询生产计划
     *
     * @param id 生产计划主键
     * @return 生产计划
     */
    @Override
    public ProductionAndSaleVo selectProductionPlanById(Long id)
    {
        return productionPlanMapper.selectProductionPlanById(id);
    }

    /**
     * 查询生产计划列表
     *
     * @param productionPlan 生产计划
     * @return 生产计划
     */
    @Override
    public List<ProductionPlan> selectProductionPlanList(ProductionPlan productionPlan)
    {
        return productionPlanMapper.selectPlansAll(productionPlan);
    }

    /**
     * 新增生产计划
     *
     * @param productionPlanAndSaleVo 生产计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProductionPlan(ProductionAndSaleVo productionPlanAndSaleVo)
    {
        ProductionPlan productionPlan = new ProductionPlan(
                null,productionPlanAndSaleVo.getSaleId(),productionPlanAndSaleVo.getPlanName(), productionPlanAndSaleVo.getPlanDescribe(),
                productionPlanAndSaleVo.getUserId(), productionPlanAndSaleVo.getPlanNo(), 0L,
                null,productionPlanAndSaleVo.getPlanStartTime(),
                productionPlanAndSaleVo.getPlanEndTime(), null
        );
        productionPlan.setCreateTime(DateUtils.getNowDate());
        int planNum = productionPlanMapper.insertProductionPlan(productionPlan);
        int i = 0;
        if (planNum == 1){
            for (ProductionPlanDetail productionPlanDetail : productionPlanAndSaleVo.getProductionPlanDetails()) {
                i++;
                productionPlanDetail.setPlanId(productionPlan.getId());
                productionPlanDetail.setCreateTime(DateUtils.getNowDate());
                planNum += productionPlanDetailMapper.insertProductionPlanDetail(productionPlanDetail);
            }
        }
        return planNum - i == 1? 1: 0;
    }

    @Transactional
    @Override
    public int updateProductionPlanAndDetail(ProductionAndSaleVo productionPlanAndSaleVo) {
        ProductionPlan productionPlan = new ProductionPlan(
                productionPlanAndSaleVo.getId(),productionPlanAndSaleVo.getSaleId(),productionPlanAndSaleVo.getPlanName(),
                productionPlanAndSaleVo.getPlanDescribe(), productionPlanAndSaleVo.getUserId(),
                productionPlanAndSaleVo.getPlanNo(), 0L,
                null,productionPlanAndSaleVo.getPlanStartTime(),
                productionPlanAndSaleVo.getPlanEndTime(), null
        );
        productionPlan.setUpdateTime(DateUtils.getNowDate());
        int planNum = productionPlanMapper.updateProductionPlan(productionPlan);
        int i = 0;
        if (planNum == 1){
            for (ProductionPlanDetail productionPlanDetail : productionPlanAndSaleVo.getProductionPlanDetails()) {
                i++;
                productionPlanDetail.setUpdateTime(DateUtils.getNowDate());
                planNum += productionPlanDetailMapper.updateProductionPlanDetail(productionPlanDetail);
            }
        }
        return planNum - i == 1? 1: 0;
    }

    /**
     * 修改生产计划
     *
     * @param productionPlan 生产计划
     * @return 结果
     */
    @Override
    public int updateProductionPlan(ProductionPlan productionPlan)
    {
        productionPlan.setUpdateTime(DateUtils.getNowDate());
        //计划完成就将销售订单改为已生产
        if (productionPlan.getStatus()==4){
            productionPlan.setEndTime(DateUtils.getNowDate());
            ProductionAndSaleVo productionAndSaleVo = productionPlanMapper.selectProductionPlanById(productionPlan.getId());
            SaleOrder saleOrder = new SaleOrder();
            saleOrder.setId(productionAndSaleVo.getSaleId());
            saleOrder.setStatus(6L);
            saleOrderMapper.updateSaleOrder(saleOrder);
        }
        return productionPlanMapper.updateProductionPlan(productionPlan);
    }

    /**
     * 批量删除生产计划
     *
     * @param ids 需要删除的生产计划主键
     * @return 结果
     */
    @Override
    public int deleteProductionPlanByIds(Long[] ids)
    {
        return productionPlanMapper.deleteProductionPlanByIds(ids);
    }

    /**
     * 删除生产计划信息
     *
     * @param id 生产计划主键
     * @return 结果
     */
    @Override
    public int deleteProductionPlanById(Long id)
    {
        return productionPlanMapper.deleteProductionPlanById(id);
    }
}
